package com.niit.musicserve.config;

import com.niit.musicserve.Intercepoter.JwtInterceptor;
import com.niit.musicserve.Intercepoter.RoleInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 解决跨域问题
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	
	@Autowired
	private JwtInterceptor jwtInterceptor;
	@Autowired
	private RoleInterceptor roleInterceptor;
	

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		
		// 注册JWT拦截器
		registry.addInterceptor(jwtInterceptor)
			.addPathPatterns("/admin/**", "/consumer/**")
			.excludePathPatterns("/admin/login/status", "/consumer/login", "/consumer/register")
		        .excludePathPatterns("/admin/consumer/**","/admin/song/**", "/admin/singer/**", "/admin/rank/**","/consumer/song/**", "/consumer/singer/**", "/consumer/rank/**", "/consumer/album/**")
			.excludePathPatterns("/img/**", "/res/**");
		//角色拦截器
		registry.addInterceptor(roleInterceptor)
			.addPathPatterns("/admin/**", "/consumer/**")
			.excludePathPatterns("/admin/login/status", "/consumer/login", "/consumer/register")
			.excludePathPatterns("/consumer/song/**", "/consumer/singer/**", "/consumer/rank/**","/consumer/album/**");
	}
}
